<?php
/**
 * Handles the venue line-up
 * 
 * @author Magnus Stoveland <mstoveland@gmail.com>
 * 
 */

class Inputlist 
{
    /**
     * Parses $url for mateches to $regex pattern
     *
     * @param string $url
     * @param string $regex pattern
     * @return array with Music-instances
     */
    function parse($venue_id) {
        // Fetch from DB
	    $query = "SELECT * FROM venues WHERE id=".$venue_id;
	    $db = Database::getInstance();
		$result = $db->query($query);
	    $venue = mysql_fetch_assoc($result);
        $file = Inputlist::_file_pointer($venue['url'], $venue['request_type']);
        if(preg_match_all("/".$venue['regex']."/siU", $file, $matches)) {
            if(count($matches) < 3) {
                Inputlist::_raiseParseLock($venue_id, $venue['url']);
            }
            foreach($matches[0] as $key => $match) {
                $venueMusic = new VenueMusic();
                $venueMusic->artist = $matches[2][$key];
                // Strip nationality parenthesis [is there a better solution?]
                if(strstr($venueMusic->artist, " (")) {
                    $temp = explode(" (", $venueMusic->artist);
                    $venueMusic->artist = $temp[0];
                }
                $venueMusic->url = $matches[1][$key];
                // Adds host if relative url [Could this be tuned?]
                if(!strstr($venueMusic->url, "http://")) {
                    preg_match('@^(?:http://)?([^/]+)@i', $url, $match);
                    $host = "http://".$match[1];
                    $venueMusic->url = $host.$venueMusic->url;
                }
                
                // Add similar
                // Checks for possible split of (artist) names.
                $split_checked_artist = Lastfm::_split_check($venueMusic->artist, array(" & ", " og ", " + "));
	            if(count($split_checked_artist) > 1) {
	                // Do extra stuff if artistname was splitted. Mention artist-name together with match-prercentage?
	            }
	            $similar_artists = array();
	            foreach($split_checked_artist as $splitted_artist) {
	                $similar_artists = Lastfm::parse(urlencode($splitted_artist));
                    array_unshift($similar_artists, array("100", "", $venueMusic->artist));
                    $venueMusic->similar = $similar_artists;
	            } 
                $music_list[] = $venueMusic;
            }
        } else {
            Inputlist::_raiseParseLock($venue_id, $venue['url']);
        }
        return $music_list;
    }
    
    function _file_pointer($url, $method = "") {
        if ($method == "curl") {
            // Create a new cURL resource
            $ch = curl_init();
            // Set URL and other appropriate options
            curl_setopt($ch, CURLOPT_URL, $url);
            curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
            curl_setopt($ch, CURLOPT_HEADER, 0);
            $file = curl_exec($ch);
            if(!$file) {
                trigger_error("Unable to open url: <strong>".$url."</strong>",E_USER_ERROR);
            }
            curl_close($ch);
        } else {
            ini_set('allow_url_fopen', 'on');
            $fp = @fopen($url, "r");
            if ($fp) {
                $file = file_get_contents($url);
            } else {
                trigger_error("Unable to open url: <strong>".$url."</strong>",E_USER_ERROR);
            }
            fclose($fp);
        }
        return $file;
    }
 
    function _raiseParseLock($venue_id, $venue_url) {
        // Temporarily hide the venue until regex is fixed
        $query = "UPDATE venues SET active = 0 WHERE id = ".$venue_id;
        $db = Database::getInstance();
	    $result = $db->query($query);
	    
	    // Notify admins
        mail("mstoveland@gmail.com", "musiceye praseLock", "Venue: ".$venue_id."\n\n".$venue_url);
    }
}
?>